using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace NewTcGui
{
	public struct MountPoint
	{
		public string dev;
		public string dir;
		public string parameters;
	}
	
	public class TcConfig
	{
		public List<MountPoint> mountPoints;
		public bool samePasswd;
		public string tcPath;
		
		public TcConfig()
		{
			mountPoints = new List<MountPoint>();
			samePasswd = true;
		}
		
		public bool ReadConfigFile()
		{
			mountPoints.Clear();
			
			//bulding path to config file string
			StringBuilder sb = new StringBuilder();
			sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
			sb.Append(Path.DirectorySeparatorChar); 
			sb.Append(".tcgui");
			
			if (!File.Exists(sb.ToString())) 
			{
				System.Console.WriteLine("Config file do not exists!");
				return false;
			}

			FileStream fs = new FileStream(Path.GetFullPath(sb.ToString()), FileMode.Open, FileAccess.Read);
			StreamReader sr = new StreamReader(fs);
			
			string line = sr.ReadLine();
			if (line[0] != '#') 
			{
				throw new FileNotFoundException("Bad or damaged config file");
			}
			
			if ((line = sr.ReadLine()) == "#MountPoints") 
			{
				line = sr.ReadLine();
				while(line[0] != '#')
				{
					MountPoint mp;
					int pos = line.IndexOf(" ");
					mp.dev = /*Path.GetFullPath*/(line.Substring(0, pos));
					line = line.Remove(0, pos);
					pos = line.IndexOf(" ");
					mp.dir = /*Path.GetFullPath*/(line.Substring(0, pos));
					line = line.Remove(0, pos);
					mp.parameters = line.Trim();
					
					mountPoints.Add(mp);
					
					line = sr.ReadLine();
				}
				
				if (line == "#Options") 
				{
					while (!sr.EndOfStream) 
					{
						line = sr.ReadLine();
						
						if (line.Contains("samePasswd="))
						{
							int pos = line.IndexOf("=");
							if (line.Length > pos+1) 
							{
								samePasswd = bool.Parse(line.Substring(pos+1).ToLower());
							}
						}
						
						if (line.Contains("tcPath=")) 
						{
							int pos = line.IndexOf("=");
							if (line.Length > pos+1) 
							{
								tcPath = line.Substring(pos+1);
							}
						}
					}
				}
				
				sr.Close();
				fs.Close();
			}
			return true;
		}
		
		public void WriteConfigFile()
		{
			StringBuilder sb = new StringBuilder();
			sb.Append( Environment.GetFolderPath(Environment.SpecialFolder.Personal));
			sb.Append(Path.DirectorySeparatorChar); 
			sb.Append(".tcgui");
			FileStream fs = new FileStream(Path.GetFullPath(sb.ToString()), FileMode.Create, FileAccess.Write);
			StreamWriter sw = new StreamWriter(fs);
			
			sw.WriteLine("#TrueCrypt Mounting GUI configuration file");
			sw.WriteLine("#MountPoints");
//			sb.Remove(0, sb.Length);
			foreach (MountPoint mp in mountPoints)
			{
				sw.WriteLine(String.Format("{0} {1} {2}", mp.dev, mp.dir, mp.parameters));
			}
			
			sw.WriteLine("#Options");
			sw.WriteLine(String.Format("samePasswd={0}", samePasswd.ToString().ToLower()));
			sw.WriteLine(String.Format("tcPath={0}", tcPath));
						
			sw.Close();
			fs.Close();
		}
	}
}
